﻿using System;
using System.Collections.Generic;
using System.Data.SqlServerCe;
using System.Linq;
using Loki.Engine;
using Luna.Data.Project;
using NUnit.Framework;

namespace Luna.Data.Tests
{
    public class ProjectTest : AssertionHelper
    {
        public IProjectData Project1 { get; set; }
        public IProjectData Project2 { get; set; }
        public IProjectData Project3 { get; set; }

        private bool AreSame(IProjectData P_Cat1, IProjectData P_Cat2)
        {
            bool b = true;
            b &= P_Cat1.PK == P_Cat2.PK;
            b &= P_Cat1.Code == P_Cat2.Code;
            b &= P_Cat1.Name == P_Cat2.Name;
            b &= P_Cat1.Description == P_Cat2.Description;
            b &= P_Cat2.Budget == P_Cat2.Budget;
            b &= P_Cat1.Start == P_Cat2.Start;
            b &= P_Cat1.End == P_Cat2.End;
            return b;
        }

        public IProjectDataProvider Data { get; set; }

        private IProjectData Create(string P_Name)
        {
            Random L_Rand = new Random(DateTime.Now.Millisecond);
            IProjectData L_Return = Data.Create<IProjectData>();
            L_Return.PK = Guid.NewGuid();
            L_Return.Code = P_Name + "_Code";
            L_Return.Name = P_Name + "_Name";
            L_Return.Description = P_Name + "_Description";
            L_Return.Budget = L_Rand.Next(1000) + 0.25m;
            L_Return.Start = DateTime.Today;
            L_Return.End = DateTime.Today.AddDays(1);
            return L_Return;
        }

        [SetUp]
        public void SetUp()
        {
            Data = ContextHelper.GetDataModel<LunaDataModel>().Project;

            Project1 = Create("Project1");
            Project2 = Create("Project2");
            Project3 = Create("Project3");

            //CleanTable();
        }

        [TearDown]
        public void CleanTable()
        {
            IEnumerable<IProjectData> L_Categories = Data.GetAllProjects();
            foreach (var L_Cat in L_Categories)
            {
                Data.DeleteProject(L_Cat.PK);
            }
        }

        [Test]
        public void TestInsertAndGet()
        {
            Data.SaveProject(Project1, new IProjectTaskData[] { });
            IProjectData L_Project = Data.GetProject(Project1.PK);
            Expect(AreSame(Project1, L_Project), Is.True);
        }

        [Test]
        public void TestUpdateAndGet()
        {
            Data.SaveProject(Project1, new IProjectTaskData[] { });
            Project1.Name = "Project1_Name_Modified";
            Data.SaveProject(Project1, new IProjectTaskData[] { });

            IProjectData L_Project = Data.GetProject(Project1.PK);
            Expect(AreSame(Project1, L_Project), Is.True);

            Expect(L_Project.Name, Is.EqualTo("Project1_Name_Modified"));
        }

        [Test]
        public void TestInsertAndGetAll()
        {
            Data.SaveProject(Project1, new IProjectTaskData[] { });
            Data.SaveProject(Project2, new IProjectTaskData[] { });
            Data.SaveProject(Project3, new IProjectTaskData[] { });

            int L_Count = Data.GetAllProjects().Count();
            Expect(L_Count, Is.EqualTo(3));
        }

        [Test]
        public void TestInsertAndDelete()
        {
            Data.SaveProject(Project1, new IProjectTaskData[] { });
            Data.SaveProject(Project2, new IProjectTaskData[] { });
            Data.DeleteProject(Project1.PK);

            int L_Count = Data.GetAllProjects().Count();
            Expect(L_Count, Is.EqualTo(1));
            IProjectData L_Project = Data.GetProject(Project1.PK);
            Expect(L_Project, Is.Null);
        }

        //[Test]
        public void Upgrade()
        {
            var dest = @"C:\Boulot\loki\Dev\ProjectExtension\Luna\Luna.Data.SqlCompact\database2.sdf";
            var source = @"C:\Boulot\loki\Dev\ProjectExtension\Luna\Luna.Data.SqlCompact\database.sdf";
            using (var engine = new SqlCeEngine(string.Format(@"Data Source={0};Persist Security Info=False", source)))
            {
                engine.Upgrade(string.Format(@"Data Source={0};Persist Security Info=False", dest));
            }
        }
    }
}
